<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hShowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" default-expand-all>
          <template v-slot="scope">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">

                  <el-col>{{ scope.data.manager }}</el-col>

                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hShowAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="scope.data.children.length == 0"
                          @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                        <!-- <el-dropdown-item v-if="" @click.native="test(scope)">111</el-dropdown-item> -->
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="showDialog" width="30%" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <add-or-edit v-if="showDialog" :id="curId" :isEdit="isEdit" :originList="originList" @success="hSuccess"
        @close="hClose()"></add-or-edit>
    </el-dialog>
    <!-- <el-dialog title="添加或编辑" :visible.sync="showDialogEdit" width="30%" :close-on-click-modal="false"
      :close-on-press-escape="false">
      <add-or-edit v-if="showDialogEdit" :id="curId" :isEdit="isEdit" @success="hSuccess"></add-or-edit>
    </el-dialog> -->
  </div>
</template>

<script>
import { deleteDepartmentById, getDepartmentList } from '@/api/departments';
import { tranListToTreeData } from '@/utils';
import addOrEdit from './deptDialog.vue'

export default {
  data() {
    return {
      // 依赖一份树形数据
      // list: [{
      //   name: '财务部',
      //   manager: '刘备',
      //   children: [
      //     {
      //       name: '财务核算部',
      //       manager: '张飞',
      //       children: [
      //         {
      //           name: '税务部',
      //           manager: 'forlord'
      //         }
      //       ]

      //     },
      //     {
      //       name: '税务核算部',
      //       manager: '关羽'
      //     }
      //   ]
      // }],
      list: [],
      showDialog: false,
      // showDialogEdit: false,
      curId: '',
      isEdit: false,
      originList: []

    }
  },
  components: {
    addOrEdit
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    handleNodeClick(data) {
      console.log(data);
    },

    async loadDepartments() {
      try {
        let res = await getDepartmentList()
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            code: item.code,
            name: item.name,
            pid: item.pid,
          }
        })
        this.list = tranListToTreeData(res.data.depts)

      } catch (error) {
        console.log(error);
      }

    },
    hShowAdd(id) {
      this.isEdit = false
      this.showDialog = true
      this.curId = id
    },
    hShowEdit(id) {
      this.isEdit = true
      this.showDialog = true
      this.curId = id
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const res = await deleteDepartmentById(id)
          console.log(res);
          this.$message({
            type: 'success',
            message: res.message
          });
          this.loadDepartments()
        } catch (error) {
          this.$message({
            type: 'error',
            message: error.message
          });
        }

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    hSuccess() {
      this.showDialog = false
      // this.showDialogEdit = false
      this.loadDepartments()
    },
    hClose() {
      this.showDialog = false
    },
    test(a) {
      console.log(a);
    }
  }
};
</script>